import { observable, action, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';
const { TableStore } = WeaTableNew;
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const getLocaleLabel = WeaLocaleProvider.getLocaleLabel.bind(this, 'crm');
import { WeaTools } from 'ecCom'
import { Button, Icon, message } from "antd";
// export const getFormDate = params => {   //新建客户表单接口/
//     return WeaTools.callApi('/api/mainportal/element/demo/getData', 'GET', params);
// }
export default class SettingStore {
    @observable loading = false;
    @observable dataSource = [];
    @observable id = "";
    @observable formfiled = [];
    @observable editbatledatas = [];
    @observable paramsdatas = [];
    @observable settingid = "";
    @observable columns = [{
        title: getLabel('24986', '标题'),
        dataIndex: 'title',
        key: 'title',
        width: "50%",
        sorter: true,
    }, {
        title: getLabel('30585', '操作'),
        className: "column-text-center",
        dataIndex: 'operation',
        key: 'operation',
        width: "50%",
        render: (text, record) => (
            <span>
                <a onClick={() => {
                    this.getElementSettingForm({ id: record.key, operation: "edit" });
                    this.dialogTitle = getLabel('19480', '内容设置');
                    this.operationtype = "editSetting";
                    this.id = record.key
                    this.getbuttons();
                }}>{getLabel('68', '设置')}{record.name}</a>
                <span className="ant-divider"></span>
                <a onClick={() => {
                    this.getElementSettingForm({ id: record.key, operation: "edit" });
                    this.dialogTitle = getLabel('19480', '内容设置');
                    this.operationtype = "quickAdd"
                    this.getbuttons();
                }}>{getLabel('350', '另存为')}</a>
                <span className="ant-divider"></span>
                <a onClick={() => {
                    this.settingid = record.key;
                    this.getElementParamsList({ settingid: record.key });
                    this.dialogTitle = getLabel('84299', '元素参数设置');
                    this.operationtype = "paramsSetting"
                    this.getbuttons();
                }}>{getLabel('17632', '参数设置')}</a>
            </span>
        ),
    },];
    @observable buttons = [];
    @observable visible = false;
    @observable selectedRowKeys = [];
    @observable dialogTitle = getLabel('19480', '内容设置');
    comBj = {};
    @observable form = new WeaForm();
    @observable formitem = [];
    @observable eid = "";
    @observable operationtype = "addSetting";
    @action
    doInit = (combj, eid) => {
        this.comBj = combj;
        this.eid = eid;
        this.getElementSettingList({ eid });
        getLocaleLabel();
    }
    getbuttons = () => {
        this.buttons = [];
        if (this.operationtype == "paramsSetting") {
            this.buttons.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@d610t7@save`} type="primary" onClick={() => { this.savaElementSettingForm(); }}>{getLabel('86', '保存')}</Button >,
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@rpnaeu@cancel`} type="ghost" onClick={() => { this.setVisible(false) }}>{getLabel('201', '取消')}</Button>)
        } else {
            this.buttons.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ukxlil@save`} type="primary" onClick={() => { this.savaElementSettingForm(); }}> {getLabel('86', '保存')}</Button >,
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@0otnpd@reset`} type="primary" onClick={() => { this.resetElementSettingForm() }}>{getLabel('2022', '重置')}</Button>,
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@lq1teg@cancel`} type="ghost" onClick={() => { this.setVisible(false) }}>{getLabel('201', '取消')}</Button>)
        }

    }
    //弹框显隐设置
    @action
    setVisible = (visible) => {
        this.visible = visible;
    }
    //选择设置项checkbook
    @action
    onTableCheckSelect = (selectedRowKeys = [], selectedRows = []) => {
        this.selectedRowKeys = selectedRowKeys;
    }
    //删除设置项
    @action
    onDeleteClick = () => {
        let params = { ids: `${this.selectedRowKeys}` };
        this.deleteElementSetting(params)
    }
    //保存设置表单
    @action
    savaElementSettingForm = () => {
        if (this.operationtype == "paramsSetting") {
            // console.log(this.comBj.tableEdit.doValidate().pass)
            if (this.comBj.tableEdit.doValidate().pass) {
                let datas = this.comBj.tableEdit.state.datas;
                this.saveElementParams({ datas: JSON.stringify(datas), settingid: this.settingid });
            }
        } else {
            this.form.validateForm().then(f => {
                if (f.isValid) {
                    this.setVisible(false);
                    if (this.operationtype == "editSetting") {
                        this.saveElementSetting({ ...this.form.getFormParams(), eid: this.eid, operation: "edit", id: this.id });
                    } else {
                        this.saveElementSetting({ ...this.form.getFormParams(), eid: this.eid });
                    }
                    // console.log(this.form.getFormParams())
                } else {
                    f.showErrors();
                }
            });
        }

    }
    //重置设置表单
    @action
    resetElementSettingForm = () => {
        this.form.resetForm();
    }

    @action
    getElementSettingForm = (params = {}) => {
        this.form = new WeaForm();
        WeaTools.callApi('/api/crm/enterprise/getElementSettingForm', 'GET', params).then(result => {
            if (result.status == "success") {
                this.formitem = result.data;
                this.form.initFormFields(toJS(result.data));
                this.getbuttons();
                this.setVisible(true);
            } else {
                message.error(result.msgCode);
            }

        })
    }

    @action
    getElementSettingList = (params = {}) => {
        WeaTools.callApi('/api/crm/enterprise/getElementSettingList', 'GET', { ...params, ...this.comBj.props.params, }).then(result => {
            if (result.status == "success") {
                this.dataSource = result.data;
                this.formfiled = result.fields;
            } else {
                message.error(result.msgCode);
            }

        })
    }

    @action
    saveElementSetting = (params = {}) => {
        WeaTools.callApi('/api/crm/enterprise/saveElementSetting', 'GET', params).then(result => {
            if (result.status == "success") {
                this.getElementSettingList({ eid: this.eid });
                message.success(result.msgCode);
            } else {
                message.error(result.msgCode);
            }
        })
    }
    @action
    deleteElementSetting = (params = {}) => {
        WeaTools.callApi('/api/crm/enterprise/deleteElementSetting', 'GET', params).then(result => {
            if (result.status == "success") {
                this.getElementSettingList({ eid: this.eid });
                this.selectedRowKeys = []
                message.success(result.msgCode);
            } else {
                message.error(result.msgCode);
            }
        })
    }
    @action
    setOperationtype = (operationtype) => {
        this.operationtype = operationtype;
    }
    @action
    getElementParamsList = (params = {}) => {
        WeaTools.callApi('/api/crm/enterprise/getElementParamsList', 'GET', params).then(result => {
            if (result.status == "success") {
                this.editbatledatas = result.data;
                this.paramsdatas = result.params;
                this.visible = true;
            } else {
                message.error(result.msgCode);
            }

        })
    }
    @action
    saveElementParams = (params) => {
        WeaTools.callApi('/api/crm/enterprise/saveElementParams', 'GET', params).then(result => {
            if (result.status == "success") {
                message.success(result.msgCode);
                this.visible = false;
            } else {
                message.error(result.msgCode);
            }
        })
    }
}